package com.sky.service.impl;

import com.sky.constant.MessageConstant;
import com.sky.dto.WebListDTO;
import com.sky.entity.WebList;
import com.sky.exception.UrlExitsErrorException;
import com.sky.mapper.WebsitesMapper;
import com.sky.service.WebsitesService;
import com.sky.vo.WebListChildVO;
import com.sky.vo.WebListVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

/**
 * @author 贾晓晨
 * @version 1.0
 */
@Service
@Transactional
public class WebsitesServiceImp implements WebsitesService {
    @Autowired
    private WebsitesMapper websitesMapper;

    @Override
    public void saveFriendPost(WebListDTO webListDTO) {
        WebList webList = websitesMapper.findSiteInfoByWebUrl(webListDTO.getUrl());
        if (webList != null) {
            throw new UrlExitsErrorException(MessageConstant.EXITS_URL);
        }
        websitesMapper.saveFirendSites(webListDTO);

    }

    @Override
    public WebListVO getWebListByAdmin() {
        List<WebListChildVO> webListAdmin = websitesMapper.getWebListByAdmin();
        List<WebListChildVO> webListCommon = websitesMapper.getWebListByCommon();
        WebListVO build = WebListVO.builder()
                .pass(webListAdmin)
                        .toBePass(webListCommon).build();
        System.out.println(webListCommon);
        return build;
    }

    @Override
    public WebListVO getWebListByCommon() {
        List<WebListChildVO> webListCommon = websitesMapper.getWebListByCommon();
        WebListVO build = new WebListVO().builder().
                toBePass(webListCommon).build();
        return build;
    }

    @Override
    public List<WebListChildVO> getWebList() {
        return websitesMapper.getWebList();
    }
}
